home *** CD-ROM | disk | FTP | other *** search
- Path: erich.triumf.ca!bennett
- From: bennett@erich.triumf.ca (P.Bennett)
- Newsgroups: comp.lang.c
- Subject: Re: gets(rec->num); I don't know what I am doing wrong...
- Date: 9 Feb 1996 07:55 PST
- Organization: TRIUMF: Tri-University Meson Facility
- Distribution: world
- Message-ID: <9FEB199607551645@erich.triumf.ca>
- References: <4fempt$mjg@aphex.direct.ca>
- NNTP-Posting-Host: ftp.triumf.ca
- News-Software: VAX/VMS VNEWS 1.50
-
- In article <4fempt$mjg@aphex.direct.ca>, etoivane@direct.ca (Ed Toivanen) writes...
- >I posted the code that I wrote so far. I can't make gets(rec->id); work
- >properly, I get 6 or 7 compilation errors indicating that parameter 1 does not
- >match function prototype. Each gets() call is incorrect! What to do?
-
- First, gets() is evil, and should be avoided at all costs - use fgets()
- instead. gets() allows the user to enter as many chars as he wants, possibly
- writing well beyond the space you have allowed for his entry, overwriting other
- variables, return addresses on the stack, etc.
-
- <snip>
- >typedef struct student_record{
- > int id; /* Range: 1..99, Unique */
- > char name[NAME_LEN + 1]; /* Non-unique */
- > PROGRAM major;
- > MARK marks;
- >} STUDENT_RECORD;
- <more snips>
-
- >bool addRecord(FILE* fp, STUDENT_RECORD* rec){
- > printf("Student id\n");
- > gets(rec->id);
-
- rec->id is an int, and gets() (or fgets()) only gets strings. Use fgets() to
- get the ID into a temporary string, then atoi() to convert it to an int.
-
- Peter Bennett VE7CEI | Vessels shall be deemed to be in sight
- Internet: bennett@triumf.ca | of one another only when one can be
- Packet: ve7cei@ve7kit.#vanc.bc.ca | observed visually from the other
- TRIUMF, Vancouver, B.C., Canada | ColRegs 3(k)
- GPS and NMEA info and programs: ftp://sundae.triumf.ca/pub/peter/index.html
-
-
-
-
-
-
-
-
-